home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 May / EnigmA AMIGA RUN 18 (1997)(G.R. Edizioni)(IT)[!][issue 1997-05][EAR-CD II].iso / softwareupdate / system / amigados / advancedroutines / example6.c < prev    next >
C/C++ Source or Header  |  1996-10-10  |  8KB  |  212 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE)           Amiga C Club (ACC) */
  4. /* --------------------------           ------------------ */
  5. /*                                                         */
  6. /* Manual:  AmigaDOS                    Amiga C Club       */
  7. /* Chapter: Advanced Routines           Tulevagen 22       */
  8. /* File:    Example6.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    93-03-17                                       */
  11. /* Version: 1.1                                            */
  12. /*                                                         */
  13. /*   Copyright 1993, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This example will examine some of the "lowest" parts in  */
  21. /* AmigaDOS. It will look up and print all Assigns, Volumes */
  22. /* and Devices AmigaDOS knows about. Please note that we    */
  23. /* will dig fairly deep down into the system, and only      */
  24. /* experienced programmers are recommended to do this. I    */
  25. /* have added a lot of comments to help you, and if you cut */
  26. /* out parts of this example carefully you should be able   */
  27. /* to use it in your own programs.                          */
  28. /*                                                          */
  29. /* This example can be used with all versions of the dos    */
  30. /* library.                                                 */
  31.  
  32.  
  33.  
  34. /* Include the dos library definitions: */
  35. #include <dos/dos.h>
  36.  
  37. /* Include memory definitions: (MEMF_ANY...) */
  38. #include <exec/memory.h>
  39.  
  40. /* Now we include the necessary function prototype files:         */
  41. #include <clib/dos_protos.h>       /* General dos functions...    */
  42. #include <clib/exec_protos.h>      /* System functions...         */
  43. #include <stdio.h>                 /* Std functions [printf()...] */
  44. #include <stdlib.h>                /* Std functions [exit()...]   */
  45.  
  46.  
  47.  
  48. /* Set name and version number: */
  49. UBYTE *version = "$VER: AmigaDOS/Advanced Routines/Example6 1.1";
  50.  
  51.  
  52.  
  53. /* 1. Declare an external global library */
  54. /*    pointer to the Dos library:        */
  55. extern struct DosLibrary *DOSBase;
  56.  
  57.  
  58.  
  59. /* Declare our own functions: */
  60.  
  61. /* Our main function: */
  62. int main( int argc, char *argv[] );
  63.  
  64. /* Prints BCPL strings: */
  65. void PrintBSTR( BSTR string_bstr );
  66.  
  67.  
  68.  
  69. /* Main function: */
  70.  
  71. int main( int argc, char *argv[] )
  72. {
  73.   /* Temporary BCPL pointer used to convert BPTRs into C pointer: */
  74.   BPTR temp_bptr;
  75.  
  76.   /* Pointer to the RootNode structure: */
  77.   struct RootNode *rootnode_ptr;
  78.  
  79.   /* Pointer to a DosInfo structure: */
  80.   struct DosInfo *dos_info_ptr;
  81.  
  82.   /* Pointer to the first DosList structure: */
  83.   struct DosList *first_doslist_node;
  84.  
  85.   /* Pointer to the current (the one we are */
  86.   /* working with) DosList structure:       */
  87.   struct DosList *doslist_node;
  88.   
  89.   
  90.   
  91.  
  92.   /* 2. Get a pointer to the RootNode structure: */
  93.   rootnode_ptr = DOSBase->dl_Root;
  94.  
  95.   /* 3. Get a BCPL pointer (BPTR) to the DosInfo structure: */
  96.   temp_bptr = rootnode_ptr->rn_Info;
  97.  
  98.   /* 4. Convert the BCPL pointer into a normal C pointer: */
  99.   /* (If I say that I hate BCPL with its acquired      */
  100.   /* pointers and strings I do not exaggerate...)      */
  101.   dos_info_ptr = (struct DosInfo *) BADDR( temp_bptr );
  102.  
  103.  
  104.  
  105.   /* Before we may start to examine the DosInfo structure we    */
  106.   /* have to turn off the multitasking by calling the Forbid()  */
  107.   /* function. As soon as we have finished using the DosInfo    */
  108.   /* structure we must of course turn the multitaskin on again, */
  109.   /* by calling the Permit() function.                          */
  110.   /*                                                            */
  111.   /* Note that while the multitasking is OFF we must be very    */
  112.   /* careful so we do not try to wait for some external event.  */
  113.   /* If we try to wait for something to happen "outside" our    */
  114.   /* program we will sit and wait forever since nothing can     */
  115.   /* happen outside our program as long as the multitasking is  */
  116.   /* off. You must therefore NEVER use the Wait() or similar    */
  117.   /* functions after you have forbidden other programs to run.  */
  118.   /* As soon as we turn the multitasking on again, by using the */
  119.   /* Permit() function, we may of course start to wait for      */
  120.   /* external events.                                           */
  121.   /*                                                            */
  122.   /* A program that turns off the multitasking is interrupting  */
  123.   /* other programs. You must therefore try to turn the         */
  124.   /* multitaskin on again as soon as possible.                  */
  125.   /*                                                            */
  126.   /* With the new Release 2 you should actually use the special */
  127.   /* LockDosList() and NextDosEntry() functions instead of      */
  128.   /* using the Forbid() and Permit() functions. However, since  */
  129.   /* this program should run on all Amigas we stick to the old  */
  130.   /* methods. (See "Amiga DOS" chapter for more information on  */
  131.   /* the new LockDosList() and NextDosEntry() functions.)       */
  132.  
  133.   /* 5. Turn the multitaskin OFF: */
  134.   Forbid(); 
  135.  
  136.  
  137.  
  138.   /* 6. Scan the "DosList" nodes... */
  139.  
  140.   /* Get a BCPL pointer (BPTR) to the first "DosList" node: */
  141.   temp_bptr = dos_info_ptr->di_DevInfo;
  142.  
  143.   /* Convert the BPTR into a C pointer: */
  144.   first_doslist_node = (struct DosList *) BADDR( temp_bptr );
  145.  
  146.   /* Start with the first node: */
  147.   doslist_node = first_doslist_node;
  148.  
  149.   /* Check all nodes: */
  150.   while( doslist_node )
  151.   {
  152.     PrintBSTR( doslist_node->dol_Name );
  153.  
  154.     printf ( " - " );
  155.  
  156.     /* Print type: */
  157.     switch( doslist_node->dol_Type )
  158.     {
  159.       case DLT_DEVICE:     printf( "Device             " ); break;
  160.       case DLT_DIRECTORY:  printf( "Assign             " ); break;
  161.       case DLT_VOLUME:     printf( "Volume             " ); break;
  162.       case DLT_LATE:       printf( "Late-binding Assign" ); break;
  163.       case DLT_NONBINDING: printf( "Non-binding Assign " ); break;
  164.       case DLT_PRIVATE:    printf( "Private node       " ); break;
  165.       default:             printf( "Unknown type!      " );
  166.     }
  167.     printf ( "\n" );
  168.     
  169.     /* Get a BPTR to the next node: */
  170.     temp_bptr = doslist_node->dol_Next;
  171.     
  172.     /* Convert the BPTR into a C pointer: */
  173.     doslist_node = (struct DosList *) BADDR( temp_bptr );
  174.   }
  175.  
  176.   /* 7. Turn the multitaskin ON again: */
  177.   Permit();
  178. }
  179.  
  180.  
  181.  
  182. /* Handly little function which prints BCPL strings (BSTRs): */
  183.  
  184. void PrintBSTR( BSTR string_bstr )
  185. {
  186.   /* Temporary string pointer */
  187.   UBYTE *string_ptr;
  188.  
  189.   /* The length of the BCPL string: */
  190.   UBYTE length;
  191.  
  192.   /* Simple loop variable: */
  193.   int loop;
  194.   
  195.   
  196.   
  197.   /* Conver the BSTR into a normal C pointer to a BCPL string: */
  198.   string_ptr = BADDR( string_bstr );
  199.  
  200.   /* Get the length of the BCPL string: (A BCPL string does not */
  201.   /* contain a NULL sign in the end, but uses instead the first */
  202.   /* byte to tell how many characters the string contains. A    */
  203.   /* BCPL string (BSTR) can therefore not contain more than 255 */
  204.   /* characters.                                                */
  205.   length = string_ptr[ 0 ];
  206.  
  207.   /* Print BCPL string: */
  208.   for( loop=1; loop <= length; loop++ )
  209.     putchar( string_ptr[ loop ] );
  210. }
  211.  
  212.